/// <reference path="jquery-vsdoc.js" />
/// <reference path="jquery-ui-1.8.custom.min.js" />

Object.count = function (aObject) {
    var i = 0;
    for (var item in aObject) {
        if (aObject[item]) i++;
    }
    return i;
};

// Based on Jquery
$.prototype.comboxBindingIn = function (data, displayField, valueField) {
    var selectControl = $("<select/>");
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    this.append(selectControl);
    return this;
};
$.prototype.comboxBinding = function (data, displayField, valueField) {
    var selectControl = this.empty();
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
$.prototype.comboxBindingWithEmptyDefault = function (data, displayField, valueField, defaultEmptyText) {
    var selectControl = this.empty();
    var emptyItem = $('<option/>');
    if (defaultEmptyText) {
        emptyItem.html(defaultEmptyText);
    }
    emptyItem.appendTo(selectControl);
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
$.prototype.comboxBindingWithArray = function (data) {
    var selectControl = this.empty();
    $(data).each(function (index, item) {
        $('<option/>')
          .val(item)
          .html(item)
          .appendTo(selectControl);
    });
    return this;
};


/*---- Datetime 相关方法 --*/

/**
* 对Date的扩展，将 Date 转化为指定格式的String
* 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
* 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2007-07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2007-03-10 二 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2007-03-10 周二 08:09:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2007-03-10 星期二 08:09:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2007-7-2 8:9:4.18
*/
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        //月份
        "d+": this.getDate(),
        //日
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12,
        //小时
        "H+": this.getHours(),
        //小时
        "m+": this.getMinutes(),
        //分
        "s+": this.getSeconds(),
        //秒
        "q+": Math.floor((this.getMonth() + 3) / 3),
        //季度
        "S": this.getMilliseconds() //毫秒
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

//2011-02-03 03:05          ToFullDateTime
Date.prototype.toFullDateTime = function () {
    return this.format("yyyy-MM-dd HH:mm");
}

//02-03 03:05           ToShortDateTime
Date.prototype.toShortDateTime = function () {
    return this.format("MM-dd HH:mm");
}

//2011-02-03                   ToFullDate
Date.prototype.toFullDate = function () {
    return this.format("yyyy-MM-dd");
}

//03:05:33              ToFullTime
Date.prototype.toFullTime = function () {
    return this.format("HH:mm:ss");
}

//星期六                ToDayOfWeek
Date.prototype.toDayOfWeek = function () {
    return this.format("EEE");
}

//2011-02-03 星期六     ToFullDateAndDay
Date.prototype.toFullDateAndDay = function () {
    return this.format("yyyy-MM-dd EEE");
}

//刚刚,x天前 之类                  ToNiceTime
Date.prototype.toNiceTime = function () {
    var now = new Date();
    var totalSecond = Math.floor((now - this) / 1000);
    if (totalSecond < 60) {
        return "刚刚";
    }

    var totalMinute = Math.floor(totalSecond / 60);
    if (totalMinute < 60) {
        return totalMinute + " 分钟前";
    }

    var dayDiff = Math.floor(totalMinute / 60 / 24);
    var yearDiff = Math.floor(dayDiff / 365);
    if (dayDiff == 0) {
        var totalHour = Math.floor(totalMinute / 60);
        return totalHour + " 小时前";
    }
    if (dayDiff == 1) {
        return "昨天 " + this.format("hh:mm");
    }
    if (dayDiff == 2) {
        return "前天 " + this.format("hh:mm");
    }
    if (yearDiff == 0) {
        return this.toShortDateTime();
    }
    return this.toFullDateTime();
}


// String转换成Date格式
String.prototype.toDateTime = function () {
    if (this.indexOf(".") > 0) {
        var data = this.split('.');
        return new Date(data[0], data[1] - 1, data[2]);
    }
    else if (this.indexOf("-") > 0) {
        var data = this.split('-');
        return new Date(data[0], data[1] - 1, data[2]);
    }
    else if (this.indexOf("/") > 0) {
        var data = this.split('/');
        return new Date(data[2], data[0] - 1, data[1]);
    }
}





String.prototype.htmlEncode = function () {
    var div = document.createElement("div");
    var text = document.createTextNode(this);
    div.appendChild(text);

    return div.innerHTML;
}

///字符串匹配
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substr(0, str.length) == str)
        return true;
    else
        return false;
    return true;
}


//取出字符串头空格
String.prototype.trim = function () {
    var str = this;
    if (str.charAt(0) == " " || str.charAt(0) == "\n") {
        str = str.slice(1);
        str = Trim(str);
    }
    return str;
}

Date.prototype.toFullChineseDateFormat = function () {
    var yy = this.getFullYear();
    var mon = this.getMonth() + 1;
    var da = this.getDate();
    var day = this.getDay();

    var date = yy + "年";
    if (mon < 10) date += '0';
    date += mon + "月";
    if (da < 10) date += '0';
    date += da + "日";
    var date = date + "&nbsp星期" + "日一二三四五六".charAt(day);
    return date;
}
Date.prototype.toFullTimeFormat = function () {

    var hh = this.getHours();
    var mm = this.getMinutes();
    var ss = this.getTime() % 60000;
    ss = (ss - (ss % 1000)) / 1000;
    var clock = hh + ':';
    if (mm < 10) clock += '0';
    clock += mm + ':';
    if (ss < 10) clock += '0';
    clock += ss;

    return clock;
}


Date.prototype.toFullTimeFormatWithoutSec = function () {

    var hh = this.getHours();
    var mm = this.getMinutes();
    var ss = this.getTime() % 60000;
    ss = (ss - (ss % 1000)) / 1000;
    var clock = hh + ':';
    if (mm < 10) clock += '0';
    clock += mm;
   
    return clock;
}

Date.prototype.toEpoch = function () {
    if (!this) return null;
    return Math.round(this.getTime() / 1000);
};
Date.fromEpoch = function (epoch) {
    if (!epoch) return null;
    return new Date(epoch * 1000);
};
Date.prototype.getDifferDayCount = function (targetDate) {
    return Math.abs((this - targetDate) / 86400000);
};
Date.prototype.addDays = function (n) {
    this.setDate(this.getDate() + 0 + n);
    return this;
}
Date.prototype.addMonths = function (n) {
    this.setMonth(this.getMonth() + 0 + n);
    return this;
}